﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class BalanceCenter_AdminNews : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            if (Request.QueryString["NewType"] != null)
            {
                this.ViewState["NewType"] = Request.QueryString["NewType"].ToString();
                if (ViewState["NewType"].ToString() == "1")
                {
                    this.lblInfo.Text = "编辑新闻信息";
                }

                if (ViewState["NewType"].ToString() == "2")
                {
                    this.lblInfo.Text = "编辑优惠活动信息";
                }

                if (ViewState["NewType"].ToString() == "3")
                {
                    this.lblInfo.Text = "编辑制度介绍信息";
                }

                if (ViewState["NewType"].ToString() == "4")
                {
                    this.lblInfo.Text = "编辑培训信息";
                }
            }

            BindData();
        }
    }

    #region Web 窗体设计器生成的代码

    override protected void OnInit(EventArgs e)
    {
        isRequestLogin = true;
        RequestCompetences = Competences.BuildCompetencesList(Competences.FillContent, Competences.EditNews);
        RequestLoginPage = "BalanceCenter/AdminNews.aspx?NewType=" + Request.QueryString["NewType"];

        base.OnInit(e);
    }

    #endregion

    private void BindData()
    {
        tbNewNews.Text = PublicFunction.GetOptionsBin("滚动新闻");

        DataTable dt = DAL.Tables.T_News.Select("", "Type=" + ViewState["NewType"].ToString() + " and Userid=" + _User.ID, "[DateTime] desc");

        if (dt == null)
        {
            PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据读写错误", this.GetType().BaseType.FullName);

            return;
        }

        g.DataSource = dt;
        g.DataBind();
        gPager.Visible = (dt.Rows.Count > 0);
    }

    protected void g_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.EditItem)
        {
            CheckBox cb = (CheckBox)e.Item.Cells[2].FindControl("cbisShow");
            cb.Checked = ShoveEL._Convert.StrToBool(e.Item.Cells[5].Text, true);

            LinkButton lbdel = (LinkButton)e.Item.Cells[3].FindControl("btnDel");
            lbdel.Attributes.Add("onclick", "return confirm('" + System.Web.HttpUtility.HtmlEncode("您确信要删除此条新闻资料吗？") + "');");
        }
    }

    protected void g_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
        if (e.CommandName == "Edit")
        {
            this.Response.Redirect("AdminNewsEdit.aspx?id=" + e.Item.Cells[4].Text, true);
            return;
        }

        if (e.CommandName == "Del")
        {
            if (DAL.Tables.T_News.Delete("[id] = " + e.Item.Cells[4].Text) < 0)            
            {
                PublicFunction.GoError(ErrorNumber.Unknow, "数据删除失败", this.GetType().BaseType.FullName);
            }

            ShoveEL._Web.JavaScript.Alert(this.Page, "操作成功");
            
            BindData();

            return;
        }
    }

    protected void btnAdd_Click(object sender, System.EventArgs e)
    {
        this.Response.Redirect("AdminNewsAdd.aspx?NewType=" + ViewState["NewType"].ToString(), true);
    }

    protected void gPager_PageWillChange(object Sender, Shove.Web.UI.PageChangeEventArgs e)
    {
        BindData();
    }

    protected void gPager_SortBefore(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
    {
        BindData();
    }

    protected void btnNewSave_Click(object sender, EventArgs e)
    {
        bool Result = PublicFunction.SetOptionsBin("滚动新闻", tbNewNews.Text);

        if (Result)
        {
            ShoveEL._Web.JavaScript.Alert(this.Page, "保存成功。");
        }
        else
        {
            ShoveEL._Web.JavaScript.Alert(this.Page, "保存错误。");
        }
    }
}
